// pages/management/management.ts
var url = require("../../utils/urls");
var commons = require("../../utils/common");
var tip = require("../../utils/tip");
var modelData = require("../../utils/data");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lineData: [],  //
    lineDataIndex: '',
    totalData: [],
    monthData: [], //月度累计
    yearData: [], //年度累计
    linId: '',
    month: '2023-04',//选择月
    year: '2023', //选择年
    nowYear: '2035-01-01',
  },
  // 选择数据
  getlineData() {
    commons.get(url.lineData).then((res) => {
      let recordData = res.data.records;
      console.log(recordData, "11211");
      let obj = {};
      obj['lineName'] = '全部'
      obj['id'] = ''
      recordData.unshift(obj);
      this.setData({

        lineData: recordData,
        // linId:res.data.records.id
      })

      this.getwebHomepageStatistics() //获取总量数据
      this.getwebHomepageMonthStatistics() //获取月度数据
      this.getwebHomepageYearStatistics()

    })
  },
  // 运输总量数据
  getwebHomepageStatistics() {
    commons.post(url.webHomepageStatistics, {
      lineId: this.data.lineData.length > 0 ? (this.data.lineData[this.data.lineDataIndex] ? this.data.lineData[this.data.lineDataIndex].id : '') : ''
    }).then((res) => {
      this.setData({
        totalData: res.data
      })
    })
  },
  //获取月度概览
  getwebHomepageMonthStatistics() {
    let data = {
      time: this.data.month,
      lineId: this.data.lineData.length > 0 ? (this.data.lineData[this.data.lineDataIndex] ? this.data.lineData[this.data.lineDataIndex].id : '') : ''
    }
    commons.post(url.webHomepageMonthStatistics, data).then((res) => {
      this.setData({
        monthData: res.data
      })
    })
  },
  // 获取年度概览
  getwebHomepageYearStatistics() {
    let data = {
      time: this.data.year,
      lineId: this.data.lineData.length > 0 ? (this.data.lineData[this.data.lineDataIndex] ? this.data.lineData[this.data.lineDataIndex].id : '') : ''
    }
    commons.post(url.webHomepageYearStatistics, data).then((res) => {
      this.setData({
        yearData: res.data,
      })
    })
  },
  //选择线路
  SelectAllChange(e) {
    this.setData({
      lineDataIndex: e.detail.value,
      linId: this.data.lineData[e.detail.value].id
    })

    this.getwebHomepageStatistics(this.data.linId);
    this.getwebHomepageMonthStatistics(this.data.month, this.data.linId);
    this.getwebHomepageYearStatistics(this.data.year, this.data.linId)

    // console.log(this.data.lineDataIndex,"fffff");

  },
  //月度事件
  bindmonthChange(e) {
    this.setData({
      month: e.detail.value
    })
    let time = this.data.month
    this.getwebHomepageMonthStatistics(time, this.data.lineId)
    console.log(this.data.lineId, "val的值gggggggggggggggggggggggggg");
  },
  // 年度事件
  bindyearChange(e) {
    this.setData({
      year: e.detail.value
    })
    let time = this.data.year
    this.getwebHomepageYearStatistics()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let userInfo = wx.getStorageSync("userInfo");
    if (userInfo.roleAlias != 'boss') {
      tip.showToast('当前用户无权限', 'error', 1000)
      wx.switchTab({
        url: "../transportPlan/transportPlan",
      })
    }
    let timeGl = new Date();
    let yyyy = timeGl.getFullYear();
    // getMonth方法返回的是0-11之间的数字，代表1-12月份，若想和日历的月份时间对应，需要+1。
    let nowMonth = (timeGl.getMonth() + 1);
    nowMonth = (timeGl.getMonth() + 1);
    if (nowMonth < 10) {
      nowMonth = "0" + nowMonth
    }
    let day = (timeGl.getDate() < 10) ? ("0" + timeGl.getDate()) : (timeGl.getDate());
    this.data.nowYear = yyyy + "-" + nowMonth + "-" + day
    console.log(this.data.nowYear, '111111111111111');

    this.getlineData() //获取数据
  },
  //退出登录
  logout() {
    tip.pageDelayJump("/pages/login/login", null, 1000);
    wx.clearStorageSync()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})